﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SILAS.Entidades;
using SILAS.BL;
using Microsoft.Practices.Unity;
using System.IO;

namespace SILAS.UI.Controllers
{
    public class VisitaController : Controller
    {

        [Dependency]
        public IVisitaBL VisitaBL { get; set; }


        //
        // GET: /Visita/

        public ActionResult Index()
        {
            ViewBag.Message = "Visita";
            return View();
        }

        //
        // GET: /Visita/Details/5

        public ActionResult Details(int id)
        {
            var visi = VisitaBL.getFromVisitabyId(id);
            return View(visi);
        }

        //
        // GET: /Visita/Create

        public ActionResult Create()
        {
            var visita = new Visita();

            var ciu = new Ciudadano() { ApMaterno = "adfaf", ApPaterno = "dasfa", Nombres = "asdfa", DNI = "8899" };
            var detalle = new  DetalleVisita(){Ciudadano = ciu};

            visita.DetalleVisitas.Add(detalle);                                                                                                                                                                                                 
        
           
            return View();
        } 

        //
        // POST: /Visita/Create

        public ActionResult ListarVisitas(Int32 ? page)
        {
            var pageNumber = page ?? 1;
            var lista = VisitaBL.GetAllFromVisita(pageNumber, 10);
            return View(lista);
        }


        [HttpPost]
        public ActionResult Create(Visita visita, HttpPostedFileBase ActaVisita)
        {
            try
            {
                // TODO: Add insert logic here

                if (ActaVisita != null && ActaVisita.ContentLength >0 )
                {
                    var extencion = Path.GetExtension(ActaVisita.FileName);

                    visita.ExtensionActa = extencion;

                    int length = ActaVisita.ContentLength; 

                    byte[] buffer = new byte[length]; 

                    ActaVisita.InputStream.Read(buffer, 0, length);

                    visita.Acta = buffer;
                    
                }

                VisitaBL.agregarVisita(visita);

                return View(visita);
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Visita/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Visita/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Visita/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Visita/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


       

    }
}
